package com.cjj2016.cjj.wandoujiaanimationdemo;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cjj2016.cjj.wandoujiaanimationdemo.utils.DisplayUtil;
import com.cjj2016.cjj.wandoujiaanimationdemo.widget.MyLinearLayout;
import com.cjj2016.cjj.wandoujiaanimationdemo.widget.MyScrollView;

public class TestActivity extends AppCompatActivity {
    private static final String TAG = "TestActivity";

    private int mImageId;

    private String mAppName;

    private int itemViewMarginTop;

    MyScrollView myScrollView;

    MyLinearLayout myLinearLayout;

    ImageView iconImgView;

    LinearLayout llContent;

    int llContentEndTopNum;

    int llContentEndBottomNum;

    boolean initData=false;

    int imgOldleftmargin;
    int iconImgViewEndLeft;
    int iconImgViewEndTop;

    //rootLL的透明度
    int mRootBgInitAlpha=150;
    ColorDrawable rootbg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        DisplayUtil.hideStatusBar(getWindow());

        setContentView(R.layout.activity_test);

        Intent intent =getIntent();
        itemViewMarginTop = intent.getIntExtra("ItemViewMarginTop",0);
        mImageId = intent.getIntExtra("imageRid",0);
        mAppName = intent.getStringExtra("appName");

        LinearLayout llRoot = (LinearLayout) findViewById(R.id.ll_root_testAct);
        rootbg = (ColorDrawable) llRoot.getBackground();
        rootbg.setAlpha(mRootBgInitAlpha);    //设置背景色透明度

        myScrollView = (MyScrollView) findViewById(R.id.myscrollview_testAct);

        myLinearLayout = (MyLinearLayout) findViewById(R.id.mylinerLayout_testAct);
        myLinearLayout.InitmContentMarginTop(itemViewMarginTop);
        myLinearLayout.setOnUpdateBgListener(new MyLinearLayout.onUpataBgListener() {
            @Override
            public void onUpdate(float ratio) {
                rootbg.setAlpha((int) (mRootBgInitAlpha - mRootBgInitAlpha * ratio));
            }
        });
        myLinearLayout.setCloseListener(new MyLinearLayout.onCloseListener() {
            @Override
            public void onClose() {
                finish(); //关闭activity
                overridePendingTransition(0, 0); //去除默认动画
            }
        });

        llContent = (LinearLayout) findViewById(R.id.ll_content_testAct);

        //最终llContent显示位置的top值
        llContentEndTopNum = itemViewMarginTop - getResources().getDimensionPixelOffset(R.dimen.detail_bar_view_height);


        iconImgView = (ImageView) findViewById(R.id.imageview_ll_content_testAct);
        iconImgView.setImageResource(mImageId);

        imgOldleftmargin = getResources().getDimensionPixelOffset(R.dimen.icon_margin);

        //iconimg最总显示位置的Top值
        iconImgViewEndTop = getResources().getDimensionPixelOffset(R.dimen.iconimg_view_endTop);


        TextView tvAppName = (TextView) findViewById(R.id.textview_appname);
        tvAppName.setText(mAppName);

        initAnimationData();

    }



    private void initAnimationData() {
        myLinearLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if(!initData){
                    //最终llContent显示位置的bottom值
                    llContentEndBottomNum = myScrollView.getMeasuredHeight()- llContent.getBottom();
                    //iconimg最总显示位置的left值
                    iconImgViewEndLeft= (DisplayUtil.getScreenWidth(TestActivity.this)-iconImgView.getWidth())/2-imgOldleftmargin;

                    myLinearLayout.setmllContentMinBottom(llContent.getBottom());
                    myLinearLayout.setIsAnimation(true);

                    initData=true;
                    startAnimation();
                }
            }
        });


    }

    private void startAnimation() {
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0,1).setDuration(400);
        valueAnimator.setStartDelay(150);
        valueAnimator.start();

        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float ratio  = (float) animation.getAnimatedValue();

                int llContentTopOffset =itemViewMarginTop - (int) (ratio * llContentEndTopNum);
                int llContentBottomOffset = (int) (ratio* llContentEndBottomNum);

                int iconImgLeftOffset =imgOldleftmargin + (int) (ratio* iconImgViewEndLeft);
                int iconImgTopOffset =imgOldleftmargin +  (int)(ratio* iconImgViewEndTop);


                myLinearLayout.setAllViewOffset(llContentTopOffset,llContentBottomOffset,iconImgLeftOffset,iconImgTopOffset);


            }
        });

        valueAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                //动画结束

                LinearLayout llTitle = (LinearLayout) findViewById(R.id.ll_title);
                llTitle.setVisibility(View.VISIBLE);

                LinearLayout llBootom = (LinearLayout) findViewById(R.id.ll_bottom);
                llBootom.setVisibility(View.VISIBLE);

                myLinearLayout.setAnimationEnd(true);

            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });


    }















}
